package com.ruoyi.paiban.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.paiban.domain.*;
import com.ruoyi.xiaoshouchuku.domain.XiaoshouChuku;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.paiban.mapper.PaibanMapper;
import com.ruoyi.paiban.service.IPaibanService;
import com.ruoyi.common.core.text.Convert;

/**
 * 员工排班Service业务层处理
 * 
 * @author beilian
 * @date 2023-03-31
 */
@Service
public class PaibanServiceImpl implements IPaibanService 
{
    @Autowired
    private PaibanMapper paibanMapper;

    /**
     * 查询员工排班
     * 
     * @param id 员工排班主键
     * @return 员工排班
     */
    @Override
    public Paiban selectPaibanById(Long id)
    {
        return paibanMapper.selectPaibanById(id);
    }

    /**
     * 查询员工排班列表
     * 
     * @param paiban 员工排班
     * @return 员工排班
     */
    @Override
    public List<Paiban> selectPaibanList(Paiban paiban)
    {
        return paibanMapper.selectPaibanList(paiban);
    }

    @Override
    public List<PaibanResult2QianDuan> selectPaiban2Date(String date,String type) throws ParseException {
        List<PaibanResult2QianDuan> paibanList = new ArrayList<>();
        switch (type){
            case "1":
                List<Paiban> paibanList1 = paibanMapper.selectPaibanList2monthandyear(date+"-01",date+"-07");
                transList(paibanList, paibanList1,date+"-01",date+"-07");
                break;
            case "2":
                List<Paiban> paibanList2 = paibanMapper.selectPaibanList2monthandyear(date+"-08",date+"-14");
                transList(paibanList, paibanList2,date+"-08",date+"-14");
                break;
            case "3":
                List<Paiban> paibanList3 = paibanMapper.selectPaibanList2monthandyear(date+"-15",date+"-21");
                transList(paibanList, paibanList3,date+"-15",date+"-21");
                break;
            case "4":
                List<Paiban> paibanList4 = paibanMapper.selectPaibanList2monthandyear(date+"-22",date+"-28");
                transList(paibanList, paibanList4,date+"-22",date+"-28");
                break;
            case "5":
                List<Paiban> paibanList5 = paibanMapper.selectPaibanList2monthandyear(date+"-29",date+"-32");
                Calendar calendar = Calendar.getInstance();
                int year = Integer.parseInt(date.split("-")[0]),month = Integer.parseInt(date.split("-")[1]);
                calendar.set(Calendar.YEAR,year);
                calendar.set(Calendar.MONTH,month);
                int lastday = calendar.getMinimum(Calendar.DAY_OF_MONTH);
                calendar.set(Calendar.DAY_OF_MONTH,lastday-1);
                String datelast = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
                transList(paibanList, paibanList5,date+"-29",datelast);

                break;
            default:break;
        }
        return paibanList;
    }

    private void transList(List<PaibanResult2QianDuan> paibanList, List<Paiban> paibanList5, String start, String end) throws ParseException {
        int startdate = Integer.parseInt(start.split("-")[2]);
        int enddate = Integer.parseInt(end.split("-")[2]);
        for (int i = 0; i < enddate-startdate+1; i++) {
            String date = start.substring(0,start.length()-2)+(startdate+i);
            Date datenew = new SimpleDateFormat("yyyy-MM-dd").parse(date);
            for (Paiban value : paibanList5) {
                if (DateUtils.isSameDay(datenew,value.getDate())) {
                    PaibanResult2QianDuan paiban = new PaibanResult2QianDuan();
                    String time = new SimpleDateFormat("yyyy-MM-dd").format(value.getDate());
                    paiban.setDate(time);
                    paiban.setYuangongName(value.getYuangongName());
                    paiban.setQiandaoStatus(value.getQiandaoStatus());
                    paibanList.add(paiban);
                }
            }
            if (paibanList.size() != i+1) {
                    PaibanResult2QianDuan paiban = new PaibanResult2QianDuan();
                    paiban.setDate(date);
                    paiban.setYuangongName("");
                    paiban.setQiandaoStatus("");
                    paibanList.add(paiban);
            }
        }
    }


    /**
     * 新增员工排班
     * 
     * @param paiban 员工排班
     * @return 结果
     */
    @Override
    public int insertPaiban(Paiban paiban)
    {
        return paibanMapper.insertPaiban(paiban);
    }

    /**
     * 修改员工排班
     * 
     * @param paiban 员工排班
     * @return 结果
     */
    @Override
    public int updatePaiban(Paiban paiban)
    {
        return paibanMapper.updatePaiban(paiban);
    }

    /**
     * 批量删除员工排班
     * 
     * @param date 需要删除的员工排班主键
     * @return 结果
     */
    @Override
    public int deletePaibanByIds(String date)
    {
        return paibanMapper.deletePaibanByIds(Convert.toStrArray(date));
    }

    /**
     * 删除员工排班信息
     * 
     * @param id 员工排班主键
     * @return 结果
     */
    @Override
    public int deletePaibanById(Long id)
    {
        return paibanMapper.deletePaibanById(id);
    }

    @Override
    public List<Paiban> selectPaibanList2monthandyear(String month, String year) {
        List<Paiban> paibanList = new ArrayList<>();
        switch (month){
//            case "4":
//                String date_start = "1000-01-01";String date_end = "3333-12-31";
//                paibanList = paibanMapper.selectPaibanList2monthandyear(date_start,date_end);
//                break;
            case "1":
                String date_start = year+"-01-01";String date_end = year+"-01-31";
                paibanList = paibanMapper.selectPaibanList2monthandyear(date_start,date_end);
                break;
            case "2":
                date_start = year+"-02-01";date_end = year+"-02-31";
                paibanList = paibanMapper.selectPaibanList2monthandyear(date_start,date_end);
                break;
            case "3":
                date_start = year+"-03-01";date_end = year+"-03-31";
                paibanList = paibanMapper.selectPaibanList2monthandyear(date_start,date_end);
                break;
            case "4":
                date_start = year+"-04-01";date_end = year+"-04-31";
                paibanList = paibanMapper.selectPaibanList2monthandyear(date_start,date_end);
                break;
            case "5":
                date_start = year+"-05-01";date_end = year+"-05-31";
                paibanList = paibanMapper.selectPaibanList2monthandyear(date_start,date_end);
                break;
            case "6":
                date_start = year+"-06-01";date_end = year+"-06-31";
                paibanList = paibanMapper.selectPaibanList2monthandyear(date_start,date_end);
                break;
            case "7":
                date_start = year+"-07-01";date_end = year+"-07-31";
                paibanList = paibanMapper.selectPaibanList2monthandyear(date_start,date_end);
                break;
            case "8":
                date_start = year+"-08-01";date_end = year+"-08-31";
                paibanList = paibanMapper.selectPaibanList2monthandyear(date_start,date_end);
                break;
            case "9":
                date_start = year+"-09-01";date_end = year+"-09-31";
                paibanList = paibanMapper.selectPaibanList2monthandyear(date_start,date_end);
                break;
            case "10":
                date_start = year+"-10-01";date_end = year+"-10-31";
                paibanList = paibanMapper.selectPaibanList2monthandyear(date_start,date_end);
                break;
            case "11":
                date_start = year+"-11-01";date_end = year+"-11-31";
                paibanList = paibanMapper.selectPaibanList2monthandyear(date_start,date_end);
                break;
            case "12":
                date_start = year+"-12-01";date_end = year+"-12-31";
                paibanList = paibanMapper.selectPaibanList2monthandyear(date_start,date_end);
                break;
        }
        return paibanList;
    }

    @Override
    public Paiban selectPaibanBydate(String date) {
        Paiban paibandateList;
        paibandateList = paibanMapper.selectPaibanByDate(date);
        return paibandateList;
    }

    @Override
    public int updataqiandao(Paiban paiban) {
        return  paibanMapper.updateqiandao(paiban);
    }

    @Override
    public List<meiRiShuJu> selectMeiRi2Xiaoshou(String startDate, String endDate, String yewuyuan) throws ParseException {

        int a = startDate.hashCode();
        int b = endDate.hashCode();
        int c = yewuyuan.hashCode();

        if (a == 0) {
            long chaZhi = 604800000L;
            Date date = new Date(System.currentTimeMillis()-chaZhi);
            startDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
        }
        if (b == 0) {
            Date date = new Date(System.currentTimeMillis());
            endDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
        }
        if (c == 0) {
            yewuyuan = null;
        }

        return paibanMapper.selectMeiRi2Xiaoshou(startDate, endDate, yewuyuan);
    }

    @Override
    public List<xiaoShou2Main> selectMeiRi2xiaoShou2Main() {
        String startDate,endDate;
        Date dateE = new Date(System.currentTimeMillis());
        Date dateS = new Date((System.currentTimeMillis()-604800000));
        startDate = new SimpleDateFormat("yyyy-MM-dd").format(dateS);
        endDate = new SimpleDateFormat("yyyy-MM-dd").format(dateE);
        return paibanMapper.selectMeiRi2xiaoShou2Main(startDate,endDate);
    }

    @Override
    public List<xiaoShou2Main> selectMeiRi2XiaoShouSearch2New() {
        return paibanMapper.selectMeiRi2XiaoShouSearch2New();
    }

    @Override
    public List<kuCun2Main> selectMeiRi2XiaoShouSearch2KuCun() {
        return paibanMapper.selectMeiRi2XiaoShouSearch2KuCun();
    }

    @Override
    public List<XiaoshouChuku> selectXiaoShouSearch2YiChuKu(String name) {
        String datenow = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
        String datebefor = new SimpleDateFormat("yyyy-MM-dd").format(new Date((System.currentTimeMillis()-(7*24*3600*1000))));
        return paibanMapper.selectXiaoShouSearch2YiChuKu(name,datenow,datebefor);
    }

    @Override
    public List<XiaoshouChuku> selectXiaoShouSearch2WeiChuKu(String name) {
        String datenow = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
        String datebefor = new SimpleDateFormat("yyyy-MM-dd").format(new Date((System.currentTimeMillis()-(7*24*3600*1000))));
        return paibanMapper.selectXiaoShouSearch2WeiChuKu(name,datenow,datebefor);
    }

    @Override
    public List<XiaoshouChuku> selectXiaoShouChuKuSearch2WeiChuKu(String name) {
        return paibanMapper.selectXiaoShouChuKuSearch2WeiChuKu(name);
    }

    @Override
    public List<Paiban> selectPaibanList2after(String dateNow) {
        String[] datelist = dateNow.split("-");
        int m = Integer.parseInt(datelist[1]) + 1;
        String mon;
        if (m >= 10) {
            mon = String.valueOf(m);
        }else {
            mon = "0"+m;
        }
        String dateAfter = datelist[0]+"-"+mon+"-01";
        return paibanMapper.selectPaibanList2after(dateNow,dateAfter);
    }

    @Override
    public List<meiRiShuJu> selectMeiRi2XiaoShouSearch4Month(String dateNow) {
        return paibanMapper.selectMeiRi2XiaoShouSearch4Month(dateNow);
    }

    @Override
    public String selectUser4id(String userId) {
        return paibanMapper.selectUser4id(userId);
    }

    @Override
    public String selectUser4name(String username) {
        return paibanMapper.selectUser4name(username);
    }

    @Override
    public List<User2Sys> selectUser2name(String username) {
        return paibanMapper.selectUser2name(username);
    }

    @Override
    public boolean selectContainOpenID(String openid) {
        List<miniapp> list = paibanMapper.selectContainOpenID(openid);
        return list.size() != 0;
    }

    @Override
    public int insertMiniapp(miniapp miniapp) {
        return paibanMapper.insertMiniapp(miniapp);
    }

    @Override
    public User2Sys selectUser2Sys4id(long userid){
        return paibanMapper.selectUser2Sys4id(userid);
    }

    @Override
    public int updateUser2Sys(User2Sys user) {
        return paibanMapper.updateUser2Sys(user);
    }

    @Override
    public int updataMiniapp(miniapp miniapp) {
        return paibanMapper.updataMiniapp(miniapp);
    }

    @Override
    public List<miniapp> selectMiniapp(miniapp miniapp) {
        return paibanMapper.selectMiniapp(miniapp);
    }

}
